package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.hutool.core.util.StrUtil;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.DateUtils;
import io.renren.dao.*;
import io.renren.dto.JoinmembermDTO;
import io.renren.dto.VipDetailDto;
import io.renren.entity.*;
import io.renren.service.JoinmembermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * ${comments}
 *
 * @author Sen 524227263@qq.com
 * @since 1.0.0 2024-05-28
 */
@Service
public class JoinmembermServiceImpl extends CrudServiceImpl<JoinmembermDao, JoinmembermEntity, JoinmembermDTO> implements JoinmembermService {

    @Override
    public QueryWrapper<JoinmembermEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<JoinmembermEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Autowired
    private UcmlOrganizeDao ucmlOrganizeDao;
    @Autowired
    private UcmlContactDao ucmlContactDao;
    @Autowired
    private GiftpackagerulemDao giftpackagerulemDao;
    @Autowired
    private MembercardkindsDao membercardkindsDao;

    @Override
    public Map<String, Object> vipDetail(String traderMOID, VipDetailDto dto) {
        Map<String, Object> result = new HashMap<>();
        JoinmembermEntity info = baseDao.selectOne(new QueryWrapper<JoinmembermEntity>().eq("JoinMemberMOID", dto.getJoinMemberMoId()));
        if (info != null) {
            // 服务日期
            result.put("serviceDate", DateUtils.format(info.getBilldate(), "yyyy-MM-dd HH:mm:ss"));
            // 服务机构
            UcmlOrganizeEntity organizeEntity = ucmlOrganizeDao.selectOne(new QueryWrapper<UcmlOrganizeEntity>()
                    .select("OrgName")
                    .eq("UCML_OrganizeOID", info.getOrgFk()));
            result.put("orgName", organizeEntity != null ? organizeEntity.getOrgname() : "");
            // 接待员
            UcmlContactEntity ucmlContactEntity = ucmlContactDao.selectOne(
                    new QueryWrapper<UcmlContactEntity>().select("PersonName")
                            .eq("UCML_CONTACTOID", info.getUcmlContactFk()));
            result.put("PersonName", ucmlContactEntity != null ? ucmlContactEntity.getPersonname() : "");
            // 套餐
            List<GiftpackagerulemEntity> packageList = giftpackagerulemDao.selectList(new QueryWrapper<GiftpackagerulemEntity>()
                    .select("GiftPackageName,GiftOldAmount")
                    .eq("GiftPackageRuleMOID", info.getGiftpackagerulemFk()));
            result.put("packageList", packageList);
            // 赠品
            List<Map<String, Object>> giftList = giftpackagerulemDao.selectMaps(
                    new QueryWrapper<GiftpackagerulemEntity>()
                            .select("GiftPackageName,GiftOldAmount")
                            .eq("GiftPackageRuleMOID", info.getGiftpackagerulemFk()));
            for (Map<String, Object> item : giftList) {
                item.put("BusinessQuantity", new BigDecimal(item.get("GiftOldAmount").toString()).setScale(2, BigDecimal.ROUND_HALF_UP));
                item.put("Name", item.get("GiftPackageName"));
            }
            result.put("giftList", giftList);

            List<Map<String, Object>> vipInfoList = new ArrayList<>();
            Map<String, Object> vipInfo = new HashMap<>();
            MembercardkindsEntity membercardkindsEntity = membercardkindsDao.selectOne(new QueryWrapper<MembercardkindsEntity>()
                    .select("Name")
                    .eq("MemberCardKindsOID", info.getMembercardkindsFk()));
            vipInfo.put("name", membercardkindsEntity != null ? membercardkindsEntity.getName() : "");
            vipInfo.put("BusinessAmount", 0);
            vipInfo.put("Amount", new BigDecimal(info.getMembershipamount()).setScale(2, BigDecimal.ROUND_HALF_UP));
            vipInfoList.add(vipInfo);
            result.put("vipInfo", vipInfoList);

            // 总价
            result.put("BusinessAmount", new BigDecimal(info.getMembershipamount()).setScale(2, BigDecimal.ROUND_HALF_UP));
            result.put("vipDiscount", 0);
            result.put("couponDiscount", 0);
            result.put("Amount", new BigDecimal(info.getMembershipamount()).setScale(2, BigDecimal.ROUND_HALF_UP));
        }

        return result;
    }

    @Override
    public List<JoinmembermEntity> findListByJoinMemberMOID(String fk) {
        List<JoinmembermEntity> list = this.baseDao.selectList(
                new QueryWrapper<JoinmembermEntity>()
                        .select("MembershipAmount,GiftPackageRuleM_FK")
                        .eq("JoinMemberMOID", fk));
        return list;
    }
}